Hello and welcome back to part two of our user defined function session.
So far we only have one parameter in our functions but of course functions can also have many parameters.
So let's define the function power very calculate x to the power of y
so you know the function definition within the brackets.
We have the parameters X and Y and we return x to the power of y and then we can call the function by
passing the arguments 2 and 7.
And we would expect the two to the power of seven and it's 128 and before we move on I want to make
clear the difference between perimeter and argument.
So many people mix those two up and also I do this sometimes.
So here in this function x and y are paramount.
So these are the names that appear in the function definition so X and Y are our parameters and year
2 and 7 arguments all values that are passed to the functions when the function is called.
So this is a slight difference and I just want to to make it clear and if you mix it up it's actually
not so serious.
So everybody understands what you mean by saying argument or parameter.
And in this example here.
So 2 and 7 are two positional arguments because Python automatically assumes that two stands for X and
seven for Y because exists the first parameter and y is the second parameter and consequently two is
the argument for the first parameter and 7 the argument for the second parameter and you can also make
this more explicit.
And by saying okay exist too and Y is seven.
And in this case 2 1 7 are so-called keyword arguments because you explicitly say okay.
And my part of me to exist 2 and my parameter y is 7
so in this case of course the same side and what you can do you can change the position of the keyword
arguments.
So instead of saying X is true into y 7 you can also say y 7 and X is 2 that's the problem.
So there's no danger that the Python could mix it up.
Now what if we use a keyword argument for the parameter X and a persistent argument for parameter y.
So let's look what we get here.
And here we get the error message position argument follows keyword argument so whenever you use a mix
of a keyword and positional arguments the positional arguments have to come first.
So you must not start with the keyboard arguments otherwise to get an error message.
But what you can do you can pass them the positional argument to for the parameter X and then apply
the keyword argument 7 to about parameter y.
So this works and what we can also do we can define default values for all parameters so we can define
the function power to with the default values to for our paramedics and seven for our parameter y
and then we can call our function without any arguments over so with the default values
and of course we can also call our function with the new values and to positional arguments.
So three for our perimeter except to follow our perimeter by
and instead of using two positional arguments 3 and 2 we can also use to keyword arguments.
X equals three and Y equals two.
So this is more explicit and they are actually better to read for everyone
now what they fear only wanted change parameter X so we have by default x equals two and Y equals seven.
And if we say that okay except B three then Python automatically assumes the default value for y.
So this works in three to the power of seven and if you only want to change X you can also take a new
position the argument for our parameter X so if you only pass here the argument three paces automatically
assumes that three it stands for X and we have the default value 7 for y and this gives them the same
result.
Now what if we only want to change our parameter y.
So if we use a positional argument for why and place it here at the beginning and Python has a problem
the cost Python thinks okay.
3 stands for X and then if you define x equals 2 then our function got multiple values for the argument
X
and same as before.
So if we start with them the keyword argument for x x equals 2 and then assume that Y is 3.
This also does not work.
Because position arguments.
Uh here.
Um.
3 for y follows and the keyboard argument and this does not work.
But what we can do we can just say okay our Y is 3 and 4 x we want to have our default value networks
and we can make it more explicit by passing the 2 keyboard arguments or saying Okay X is 2 It's the
same s and the default value and why should be 3 and in my opinion this is definitely best practice
because everybody sees and knows at a first glance.
Yeah.
The arguments for for the parameters exist to win.
Y is 3
all right.
So now coming to a completely different topic we can also assign a variety to our return value.
So yeah our function power to returns um the value 8 in this case and we can directly assign the arrival
set so set should be 8 and this correct.
All right.
So this last part 2 of user defined functions and in the next session we will learn that a function
can also return multiple values and we will also have a look at uh yeah two case studies so hope to
see you there by.